{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 总概括\n",
    "首先，我们将基本逻辑关系分为几类：对应（集合中元素a映射到b的基本关系）\\相关：成绩好的人更可能收入高（概率分布），我们需要程序区分并学习这些关系\n",
    "# 对应关系\n",
    "比如：小明爱吃苹果，小红爱吃梨子，小华爱吃香蕉，小绿爱吃橙子，我们将人名和水果名随机编码，然后需要学习这些对应。\n",
    "\n",
    "#下面解决一个简单问题：\n",
    "100(num_human)个人喜欢100(num_fruit)种水果，人名和水果名随机编码为fruit，human，我们观察到sample_time=150个样本，试图\n",
    "从样本中还原“谁喜欢吃什么水果”这个一一对应真值映射表/“百分之百的、一对一的概率转移矩阵”\n",
    "\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ （试想，我们为了准确性是否可以总是假设任意两变量之间总是借助概率转移矩阵进行转移的，但在后期优化中，在“复习/整理”阶段中，将一一映射概率转移矩阵加速为直接转移；\n",
    "+ 或者，我们还有另外一种方式，建立一个转移树，x1,x2,...在左，y1,y2,...在右，每读取一个样本，就检查是否有此联系，若有则该连接加一（加强连接），若无，则创建联系，\n",
    "初始计数为1）。\n",
    "+ 哦不，我想，，我们应该直接创建一个概率转移矩阵，然后不断迭代每一个取样，更新转移概率值，但是，我们要使其高效？\n",
    "+ 但是，为什么不直接统计整体的转移概率P（Y|X）呢？计算每一个P（y1|x1）=n(x1,y1)|n(x1).\n",
    "+ 我想，我们并不是主要为了概率问题而设计的，而是逻辑上的一一对应。如果是直接选择概率转移，那么明显，我们只能选择最大概率的转移项（最大似然估计/先验估计），其错误率是无法消除的。\n",
    "因此，我们只找到百分之百或接近百分之百的一一映射（前提是样本不能让极端case占据太高比例，实际情况下多数观察到的是非极端case，我想，是成立的）\n",
    "\n",
    "我们想做的是在一定的抗极端例子下的逻辑判断树挖掘，对于极端例子进行特例挖掘，挖掘特例之间隐藏的共性与同其余非特例的差异，整个逻辑树应该是一个流程图或者程序，其中包含了多种可杂交、\n",
    "可并联串联、同时投票的预测模型。\n",
    "这个逻辑树可以非常方便地进行自监督学习。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 真值表生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "# 创造一个水果集合fruit,由于set不方便，用list\n",
    "fruit = []\n",
    "num_fruit = 100     #水果总数\n",
    "maxval_fruit=10000  #水果最大编号\n",
    "assert maxval_fruit>num_fruit   #水果总数小于最大编号\n",
    "while len(fruit) != num_fruit:  #当已经编号的水果数还不够时\n",
    "    fruit_member = random.randint(0,maxval_fruit)   #随机编号\n",
    "    if fruit_member not in fruit:   \n",
    "        fruit.append(fruit_member)\n",
    "print(len(fruit))\n",
    "\n",
    "# 创造一个100人集合human,\n",
    "human = []\n",
    "num_human = 100\n",
    "maxval_human=10000\n",
    "assert maxval_human>num_human\n",
    "while len(human) != num_human:\n",
    "    human_member = random.randint(0,10000)\n",
    "    if human_member not in human:\n",
    "        human.append(human_member)\n",
    "\n",
    "# 创建什么水果和喜欢吃的人的对应，作为100个对应的基本事实\n",
    "Groundtruth_human_fruit={fruit[i]:human[i] for i in range(100)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 取样器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从基本事实重复抽样,获得抽样集合/观察150个样本sample\n",
    "Groundtruth_human_fruit_list=list(Groundtruth_human_fruit.items())\n",
    "sample=[]\n",
    "times=0\n",
    "sample_time=150\n",
    "while times<sample_time:\n",
    "    i=random.randint(0,99)\n",
    "    sample.append(Groundtruth_human_fruit_list[i])\n",
    "    times+=1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 预测器/记录器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "79 150 100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(2075, 9473),\n",
       " (8759, 2230),\n",
       " (2412, 4061),\n",
       " (9790, 7519),\n",
       " (762, 9281),\n",
       " (8048, 4581),\n",
       " (7650, 5528),\n",
       " (6703, 3369),\n",
       " (6165, 7624),\n",
       " (610, 9864)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从sample中还原对应事实\n",
    "recovery_truth=[]\n",
    "for i in sample:\n",
    "    if i not in recovery_truth:\n",
    "        recovery_truth.append(i)\n",
    "print(len(recovery_truth),len(sample),len(Groundtruth_human_fruit))\n",
    "recovery_truth[:10]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这样的真值生成-采样-记录还原，同直接从（X,Y）中学习有何不同呢？\n",
    "\n",
    "1.如果不存在噪声，一对一，前者没有任何优点。但是注意，在从（X,Y）学习中几乎很少假设真值表的存在，即总是希望不借助真值表而利用一种函数（回归利用函数模型、线性XY模型、随机森林利用大小比较转移函数、SVM利用线性或近线性、神经网络利用线性+非线性注意力强调近似）近似一种分布，即直接从X不经过查表算出Y。\n",
    "但是这种映射可能是高度非线性，或者不简单近似于任何一种分布（实际上如果是按照真值表进行概率转移，且转移概率为百分之百，那么一定可以用一个高维函数近似，但训练阶段显得复杂，而且\n",
    "会过多提前用掉前面的网络层数，效率低下，且能否训练拟合尚无定论）。\n",
    "\n",
    "1.1按照真值映射-对应逻辑是极为简单的基本逻辑之一，将其过度复杂化或近似肯定不对。\n",
    "\n",
    "1.2由于标签的非对应性，因此学习他们之间的映射真值表是非常重要的（转移概率和转移学习）\n",
    "\n",
    "2.如果存在噪声，一对一，可以进行投票。\n",
    "2.如果存在噪声和多对多，可以总结转移概率和总结真值。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以发现100个真实对应中，随机采样150个，还原了76个（其实是采样覆盖了76个，无噪声下100%还原样本中的对应）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "150"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算抽样分布的概率\n",
    "sample_time\n",
    "#..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加噪声和概率模型，认定一个水果只能对应一个人\n",
    "def generate_data_model():\n",
    "    X=[]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env4dl",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
